<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8" />
  <title>语音翻译交互界面</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      font-family: -apple-system, BlinkMacSystemFont, sans-serif;
      height: 100vh;
      overflow: hidden;
      background: black;
      position: relative;
    }

    .background-blur {
      position: absolute;
      top: 0; left: 0; right: 0; bottom: 0;
      background: rgba(0, 0, 0, 0.4);
      backdrop-filter: blur(12px);
      -webkit-backdrop-filter: blur(12px);
      z-index: 0;
    }

    .app-container {
      position: absolute;
      top: 5%;
      left: 50%;
      transform: translateX(-50%);
      width: 280px;
      background: rgba(255, 255, 255, 0.1);
      backdrop-filter: blur(15px);
      -webkit-backdrop-filter: blur(15px);
      border-radius: 16px;
      padding: 10px;
      color: #fff;
      display: flex;
      flex-direction: column;
      box-shadow: 0 0 8px rgba(0,0,0,0.3);
      z-index: 1;
    }

    .header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 6px;
    }

    .tabs {
      display: flex;
      gap: 4px;
      font-size: 11px;
    }

    .tabs div {
      padding: 3px 6px;
      border-radius: 12px;
      background: rgba(255,255,255,0.25);
      cursor: pointer;
    }

    .tabs .active {
      background: #fff;
      color: black;
    }

    .settings-btn {
      background: rgba(255,255,255,0.3);
      border: none;
      border-radius: 50%;
      width: 24px;
      height: 24px;
      color: white;
      font-size: 14px;
      cursor: pointer;
      user-select: none;
      touch-action: none;
    }

    .settings-panel {
      position: absolute;
      top: 50px;
      right: 10px;
      background: rgba(0, 0, 0, 0.7);
      padding: 12px;
      border-radius: 12px;
      color: white;
      font-size: 13px;
      display: none;
      z-index: 10;
      width: 180px;
      box-shadow: 0 2px 8px rgba(0,0,0,0.4);
    }

    .settings-panel.active {
      display: block;
    }

    .chat-area {
      flex: 1;
      overflow-y: auto;
      margin: 6px 0;
      display: flex;
      flex-direction: column;
    }

    .bubble {
      margin: 4px 0;
      padding: 6px 10px;
      border-radius: 10px;
      max-width: 75%;
      line-height: 1.4;
      font-size: 13px;
    }

    .left {
      background: rgba(0,0,0,0.7);
      align-self: flex-start;
      color: white;
    }

    .right {
      background: #FFD44F;
      color: black;
      align-self: flex-end;
    }

    .status-line {
      display: flex;
      align-items: center;
      font-size: 13px;
      margin: 6px 0;
    }

    .dot {
      width: 10px;
      height: 10px;
      border-radius: 50%;
      background-color: red;
      margin-left: 6px;
      animation: blink 1s infinite;
    }

    @keyframes blink {
      0%, 100% { opacity: 1; }
      50% { opacity: 0.2; }
    }

    .input-bar {
      display: flex;
      background: rgba(255,255,255,0.2);
      border-radius: 12px;
      padding: 2px 4px;
      align-items: center;
    }

    .input-bar input {
      flex: 1;
      border: none;
      background: transparent;
      color: white;
      padding: 6px;
      outline: none;
      font-size: 13px;
    }

    .input-bar button {
      background-color: #00AEEF;
      color: white;
      border: none;
      padding: 4px 10px;
      border-radius: 6px;
      cursor: pointer;
      font-size: 13px;
    }

    .mic-button {
      margin-left: 4px;
      background-color: rgba(255, 255, 255, 0.3);
      border-radius: 50%;
      width: 28px;
      height: 28px;
      border: none;
      color: white;
      font-size: 14px;
      cursor: pointer;
    }
  </style>
</head>
<body>
  <div class="background-blur"></div>

  <div class="app-container">
    <div class="header">
      <div class="tabs">
        <div class="active" onclick="switchMode('a11y')">无障碍语音</div>
        <div onclick="switchMode('translate')">语话翻译</div>
        <div onclick="switchMode('text')">语音转文本</div>
      </div>
      <div class="settings-btn" id="settingbtn">⚙️</div>
    </div>

    <div class="settings-panel" id="settingsPanel">
      <div>对方语言：自动识别</div>
      <div>你的语言：自动</div>
      <div>收音来源：手机麦克风</div>
      <div>音色：男声</div>
    </div>

    <div class="chat-area" id="chat">
      <div class="bubble right">How are you<br><small>你好</small></div>
      <div class="bubble left">你好<br><small>How are you</small></div>
      <div class="bubble right">Just fine<br><small>还好</small></div>
      <div class="status-line">对方正在讲话... <div class="dot"></div></div>
    </div>

    <div class="input-bar">
      <input type="text" placeholder="输入文本..." id="textInput">
      <button onclick="sendMessage()">发送</button>
      <button class="mic-button" onmousedown="startVoice()" onmouseup="stopVoice()">🎤</button>
    </div>
  </div>

  <script>
    let currentMode = 'a11y';
    let recognition;

    document.getElementById('settingbtn').addEventListener('click', function () {
      console.log("点击设置按钮，跳转到浮动对话框页面");
      if (window.Android && window.Android.callback) {
        var msg = { type: 'settingDialog', data: '设置对话框' };
        window.Android.callback("sendMessage", JSON.stringify(msg));
      }
    });

    function switchMode(mode) {
      currentMode = mode;
      document.querySelectorAll('.tabs div').forEach(tab => tab.classList.remove('active'));
      document.querySelector(`.tabs div[onclick*="${mode}"]`).classList.add('active');
      clearChat();
      if (mode === 'a11y') addSystemBubble('无障碍语音模式已启用');
      if (mode === 'translate') addSystemBubble('语话翻译模式已启用');
      if (mode === 'text') addSystemBubble('语音转文本模式已启用');
    }

    function clearChat() {
      document.getElementById('chat').innerHTML = '';
    }

    function sendMessage() {
      const input = document.getElementById('textInput');
      const msg = input.value.trim();
      if (!msg) return;
      addBubble(msg, 'right');
      if (currentMode === 'translate') {
        setTimeout(() => {
          addBubble('（翻译）' + msg, 'left');
        }, 1000);
      }
      input.value = '';
    }

    function addBubble(text, side) {
      const chat = document.getElementById('chat');
      const bubble = document.createElement('div');
      bubble.className = `bubble ${side}`;
      bubble.innerHTML = text;
      chat.appendChild(bubble);
      chat.scrollTop = chat.scrollHeight;
    }

    function addSystemBubble(text) {
      const chat = document.getElementById('chat');
      const line = document.createElement('div');
      line.className = 'status-line';
      line.innerHTML = `${text} <div class="dot"></div>`;
      chat.appendChild(line);
    }

    function startVoice() {
      if (!('webkitSpeechRecognition' in window)) {
        alert('当前浏览器不支持语音识别');
        return;
      }
      recognition = new webkitSpeechRecognition();
      recognition.lang = 'zh-CN';
      recognition.interimResults = false;
      recognition.continuous = false;
      recognition.onresult = function (event) {
        const result = event.results[0][0].transcript;
        addBubble(result, 'right');
        if (currentMode === 'translate') {
          setTimeout(() => {
            addBubble('（翻译）' + result, 'left');
          }, 1000);
        }
      };
      recognition.start();
    }

    function stopVoice() {
      if (recognition) recognition.stop();
    }
  </script>
</body>
</html>
